using System;
using System.Collections.Generic;
using System.Linq;
using ITS.Core.Domain.Projects;
using System.Threading;
using ITS.DAL.Projects;

namespace ITS.DAL.Concrete.EF.Projects
{
    public class EFProjectRepository : IProjectRepository
    {
        private readonly EnterpriseContext _context = new EnterpriseContext();


        public IQueryable<ProjectDescription> GetProjectsByCategoryIdInCurrentCulture(int categoryID)
        {
            string currentCulture = Thread.CurrentThread.CurrentCulture.ToString();
            return _context.ProjectDescriptions.Where(x => x.LanguageID == currentCulture && x.Project.CategoryID == categoryID);
        }


        public ProjectDescription GetProjectDescriptionByIdInCurrentCulture(int id)
        {
            string currentCulture = Thread.CurrentThread.CurrentCulture.ToString();
            return _context.ProjectDescriptions.SingleOrDefault(x => x.LanguageID == currentCulture && x.ProjectID == id);
        }


        public IEnumerable<ProjectDescription> GetAllProjectDescriptions()
        {
            throw new NotImplementedException();
        }


        public IEnumerable<ProjectDescription> GetAllProjectDescriptionsInCurrentCulture()
        {
            string currentCulture = Thread.CurrentThread.CurrentCulture.ToString();
            return _context.ProjectDescriptions.Where(x => x.LanguageID == currentCulture && x.Active == true);
        }


        public IList<ProjectDescription> GetRelatedProjects(int p)
        {
            string currentCulture = Thread.CurrentThread.CurrentCulture.ToString();
            Project project = GetProjectById(p);
            return _context.ProjectDescriptions.Where(x => x.Active == true && x.Project.CategoryID == project.CategoryID && x.LanguageID == currentCulture).ToList();
        }

        public Project GetProjectById(int p)
        {
            return _context.Projects.SingleOrDefault(x => x.ProjectID == p);
        }


        public IList<ProjectCategory> GetAllProjectCategories()
        {
            return _context.ProjectCategories.Where(x => x.Active == true).ToList();
        }

        public IQueryable<ProjectDescription> GetProjectDescriptionsByCategoryID(int categoryID)
        {
            throw new NotImplementedException();
        }


        public IList<Project> GetProjectsByCategoryID(int CategoryID)
        {
            return _context.Projects.Where(x => x.CategoryID == CategoryID).ToList();
        }


        public Project GetProjectByID(int ProjectID)
        {
            return _context.Projects.Single(x => x.ProjectID == ProjectID);
        }

        public IList<ProjectDescription> GetProjectDescriptionsByProjectID(int ProjectID)
        {
            return _context.ProjectDescriptions.Where(x => x.ProjectID == ProjectID).ToList();
        }

        public void Save()
        {
            _context.SaveChanges();
        }


        public ProjectDescription GetProjectDescriptionById(int DescriptionID)
        {
            return _context.ProjectDescriptions.Single(x => x.DescriptionID == DescriptionID);
        }


        public void AddProjectImage(ProjectImage image, int ProjectID)
        {
            Project project = GetProjectById(ProjectID);
            project.ProjectImages.Add(image);
            Save();
        }


        public ProjectImage GetProjectImageByID(Guid ImageID)
        {
            return _context.ProjectImages.Single(x => x.ImageID == ImageID);
        }


        public IList<ProjectImage> GetProjectImagesByProjectID(int ProjectID)
        {
            return _context.ProjectImages.Where(x => x.ProjectID == ProjectID).ToList();
        }


        public void DeleleProjectImage(Guid ImageID)
        {
            ProjectImage image = GetProjectImageByID(ImageID);
            _context.ProjectImages.Remove(image);
            Save();
        }


        public ProjectDescription GetProjectDescriptionByProjectIdAndLanguageID(int ProjectID, string LanguageID)
        {
            return _context.ProjectDescriptions.SingleOrDefault(x => x.ProjectID == ProjectID && x.LanguageID == LanguageID);
        }


        public void AddProject(Project project)
        {
            _context.Projects.Add(project);
            Save();
        }


        public void AddProjectDescription(ProjectDescription description)
        {
            _context.ProjectDescriptions.Add(description);
            Save();
        }
    }
}
